<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 	
	$typeString = "TV Shows";
	include("includes/db_connect.php");
	$directoriesResult=mysql_query("SELECT d.id, d.computer_path, d.http_path FROM tbl_directories d, tbl_types t WHERE d.type_id=t.id AND t.type = '$typeString' ORDER BY computer_path");
	$fileExtsResult=mysql_query("SELECT f.id, f.extension, f.is_active FROM tbl_fileextensions f, tbl_types t WHERE f.type_id=t.id AND t.type='$typeString' ORDER BY is_active desc,extension");
	$themesResult=mysql_query("SELECT t.id, t.name, t.url FROM tbl_themes t, tbl_types ty WHERE t.type_id=ty.id AND ty.type='$typeString' ORDER BY name");
	$erroredTVShowsResult=mysql_query("SELECT t.id,t.filename,t.name,e.error_string FROM tbl_tvshow_episode t, tbl_errorstrings e where (t.tvdbepisode_id=-1 OR t.series_id=-1) AND t.error_id=e.id ORDER BY t.filename") or die(mysql_error());
	$scannedTVShowsResult=mysql_query("SELECT e.id, e.filename, s.name as series_name, e.name, e.season,e.episode,e.airdate FROM tbl_tvshow_episode e,tbl_tvshow_series s where e.tvdbepisode_id != -1 and e.series_id != -1 and e.series_id=s.id ORDER BY s.name,e.season,e.episode");
	$message = $_GET['message'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<LINK href="../css/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jQuery.js"></script>
<script type="text/javascript" src="../js/simpleModal.js"></script>
<script>
function scanAll(action){
	confirmWindow('Scanning TV Shows','<center>Scanning...</center>', 95, 250, false);
	
	$.ajax({
			url: '../scripts/tvShowScraper.php?action='+action,
			type: 'get',
			success: function(data) {
				$.modal.close();
				if(data.trim().substring(10,53) == 'Fatal error</b>:  Maximum execution time of'){
					alert('Scan was shutdown after a 5 minute time limit.  It didn\'t complete and will need to be ran again to finish the rest of the scan.');
				}
				location='tvshows-setup.php';
			},
			error: function(data) {
				$.modal.close();
	  			alert('Error scanning tv shows...');
			}
	});
}

function rescan(action, showId){
	$.ajax({
			url: '../scripts/tvShowScraper.php?action='+action+'&showId='+showId,
			type: 'get',
			success: function(data) {
				var resp = jQuery.trim(data);
				if(resp == 'no results'){
					alert('no results found');
				}else if(resp == 'invalid filename'){
					alert('invalid filename');
				}else if(resp == 'updated'){
					location='tvshows-setup.php';
				}else{
					confirmWindow('Multiple Results Returned',resp, 400, 500, true);
				}
			},
			error: function(data) {
	  			alert('error updating tv show...');
			}
	});
}

function selectDup(showId, seriesId){
	updateConfirm('Multiple Results Returned','updating record...', 400, 500, true);
	$.ajax({
			url: '../scripts/tvShowScraper.php?action=duplicateSelected&showId='+showId+'&seriesId='+seriesId,
			type: 'get',
			success: function(data) {
				$.modal.close();
				var resp = jQuery.trim(data);
				if(resp == 'success'){
					location='tvshows-setup.php';				
				}else{
					alert('error: '+resp);
				}
			},
			error: function(data) {
				$.modal.close();
	  			alert('Error selecting tv show...');
			}
	});
}


function confirmWindow(title,message, height, width, scrollVar) {
	var newStyle = 'height: '+(height-54)+'px;width: '+(width-12)+'px;';

	if(scrollVar){
		newStyle += ' overflow:scroll;';
	}
	
	$('div.message').attr('style',newStyle);
	$('div.header').html('<span>'+title+'</span>');
	
	$('#confirm').modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		autoPosition: 'true',
		overlayId: 'confirm-overlay',
		containerId: 'confirm-container', 
		onShow: function (dialog) {
			var modal = this;
			modal.update(height,width);

			$('.message', dialog.data[0]).append(message);

//				modal.close(); // or $.modal.close();
		}
	});
}

function updateConfirm(title,message, height, width, scrollVar) {
	var newStyle = 'height: '+(height-54)+'px;width: '+(width-12)+'px;';

	if(scrollVar){
		newStyle += ' overflow:scroll;';
	}
	
	$('div.message').attr('style',newStyle);
	$('div.header').html('<span>'+title+'</span>');
	
	$.modal.update(height,width);
	$('div.message').html(message);
}
</script>
</head>
<body>
<?php echo $message; ?>
<h4>TV Show Settings</h4><!--
<table cellpadding="2" class="rowmajor">
 <tbody><tr>
 <th class="vt"><div align="left">Theme :</div></th>
 <td><input name="name" type="text" id="name" value="<?php echo $name;?>" size="50" maxlength="512"/></td>
 </tr>
 </tbody></table>-->
 <p>&nbsp;</p>
<h4>Directories   <input type="button" name="addDirButton" value="Add" onclick="location='tvshows-directory-edit.php?action=Add'"/></h4>
<table width="70%" border="0" align="center" cellpadding="0" cellspacing="0" class="setupTable">
  <tr>
    <th width="8%">&nbsp;</th>
    <th width="8%">&nbsp;</th>
    <th width="42%">Computer Path</th>
    <th width="42%">HTTP Path</th>
  </tr>
   <?php
  $i=0;
	while ($row = mysql_fetch_array($directoriesResult)) {
	$i++;
	$class = "";
	
	if($i % 2 == 0){
		$class = 'class="alt"';
	}
  ?>
  <tr>
	<td <?php echo $class;?>><div align="center"><a href="tvshows-directory-edit.php?action=Edit&id=<?php echo $row['id'];?>">Edit</a></div></td>
  	<td <?php echo $class;?>><div align="center"><a href="tvshows-directory-edit.php?action=Del&id=<?php echo $row['id'];?>" onclick="javascript:return confirm('Are you sure you want to delete?')">Delete</a></div></td>
	<td <?php echo $class;?>><?php echo $row['computer_path'];?></td>
	<td <?php echo $class;?>><?php echo $row['http_path'];?></td>
  </tr>
  <?php 
  	}
	
	if(mysql_num_rows($directoriesResult) == 0){
  ?>
  <tr>
    <td colspan="4"><div align="center">No directories set</div></td>
  </tr>
  <?php
  }
  ?>
  <tr>
    <td class="footer" colspan="4"></td>
  </tr>
</table>
 <p>&nbsp;</p>
<h4>File Extensions <input type="button" name="addExtButton" value="Add" onclick="location='tvshows-extension-edit.php?action=Add'"/></h4>
<table width="50%" style="margin-left:auto;margin-right:auto;">
<tr>
  <td>
  Don't change this section if you don't know what you are doing.
  </td>
  </tr>
</table>
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0" class="setupTable">
  <tr>
    <th width="8%">&nbsp;</th>
    <th width="8%">&nbsp;</th>
    <th width="42%">Extension</th>
    <th width="42%">Is Active</th>
  </tr>
	<?php
  $i=0;
	while ($row = mysql_fetch_array($fileExtsResult)) {
	$i++;
	$class = "";
	
	if($i % 2 == 0){
		$class = 'class="alt"';
	}
  ?>
  <tr>
	<td <?php echo $class;?>><div align="center"><a href="tvshows-extension-edit.php?action=Edit&id=<?php echo $row['id'];?>">Edit</a></div></td>
  	<td <?php echo $class;?>><div align="center"><a href="tvshows-extension-edit.php?action=Del&id=<?php echo $row['id'];?>" onclick="javascript:return confirm('Are you sure you want to delete?')">Delete</a></div></td>
	<td <?php echo $class;?>><?php echo $row['extension'];?></td>
	<td <?php echo $class;?>><?php echo $row['is_active'];?></td>
  </tr>
   <?php 
  	}
	
	if(mysql_num_rows($fileExtsResult) == 0){
  ?>
   <tr>
    <td colspan="4"><div align="center">No file extensions set</div></td>
  </tr>
  <?php
  }
  ?>
  <tr>
    <td class="footer" colspan="4"></td>
  </tr>
</table>
<p>&nbsp;</p>
<h4>Themes  <input type="button" name="addThemeButton" value="Add" onclick="location='tvshows-theme-edit.php?action=Add'"/></h4>
<table width="85%" style="margin-left:auto;margin-right:auto;">
<tr>
  <td>
  Don't change this section if you don't know what you are doing.
  </td>
  </tr>
</table>
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0" class="setupTable">
  <tr>
  </tr>
  <tr>
    <th width="8%">&nbsp;</th>
    <th width="8%">&nbsp;</th>
    <th width="42%">Theme Name</th>
    <th width="42%">Url</th>
  </tr>
   <?php
  $i=0;
	while ($row = mysql_fetch_array($themesResult)) {
	$i++;
	$class = "";
	
	if($i % 2 == 0){
		$class = 'class="alt"';
	}
  ?>
  <tr>
	<td <?php echo $class;?>><div align="center"><a href="tvshows-theme-edit.php?action=Edit&id=<?php echo $row['id'];?>">Edit</a></div></td>
  	<td <?php echo $class;?>><div align="center"><a href="tvshows-theme-edit.php?action=Del&id=<?php echo $row['id'];?>" onclick="javascript:return confirm('Are you sure you want to delete?')">Delete</a></div></td>
	<td <?php echo $class;?>><?php echo $row['name'];?></td>
	<td <?php echo $class;?>><?php echo $row['url'];?></td>
  </tr>
  <?php 
  	}
	
	if(mysql_num_rows($themesResult) == 0){
  ?>
  <tr>
    <td colspan="4"><div align="center">No themes set</div></td>
  </tr>
  <?php
  }
  ?>
  <tr>
    <td class="footer" colspan="4"></td>
  </tr>
</table>
<p>&nbsp;</p>
<h4>TV Show Scanner</h4>
  <table width="60%" border="0" cellpadding="0" cellspacing="0" class="centerTable">
    <tr>
      <td width="36%"><input type="button" name="scanAll" value="Scan Directories for TV Shows" onclick="scanAll('scrapeAll');"/></td>
      <td width="64%"><p>Once the file extensions and directories have been setup click this button. This will scrape all the directories you have setup and search the internet for information on your tv shows. If it cant find it by filename it will still be inserted, it will just have an error.</p>
      <p>&nbsp;</p>
      <p>You can always click this after initial setup and it will add new files and remove files you have deleted.  <em>Note, this process may take several minutes.</em> </p></td>
    </tr>
  </table>
<h4>TV Shows With Errors</h4>
<table width="98%" style="margin-left:auto;margin-right:auto;">
<tr>
  <td>
  First, try pressing rescan and selecting the tv show that is the one that you intend.  If that doesn't work you may need to press the edit link and rescan by title in that page.
  </td>
  </tr>
</table>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" class="setupTable">
  <tr>
    <th width="8%">&nbsp;</th>
    <th width="7%">&nbsp;</th>
    <th width="39%">File Name </th>
    <th width="46%">Error</th>
  </tr>
  <?php
  $i=0;
	while ($row = mysql_fetch_array($erroredTVShowsResult)) {
	$i++;
	$class = "";
	
	if($i % 2 == 0){
		$class = 'class="alt"';
	}
  ?>
  <tr>
    <td <?php echo $class;?>><div align="center"><a href="movies-edit.php?action=Edit&id=<?php echo $row['id'];?>">Edit</a></div></td>
    <td <?php echo $class;?>><div align="center"><a href="#" onclick="javascript:rescan('scrapeShowByDBId', <?php echo $row['id'];?>);">Rescan</a></div></td>
    <td <?php echo $class;?>><?php echo $row['filename'];?></td>
    <td <?php echo $class;?>><?php echo $row['error_string'];?></td>
  </tr>
  <?php 
  	}
	
	if(mysql_num_rows($erroredTVShowsResult) == 0){
  ?>
  <tr>
    <td colspan="4"><div align="center">No TV shows with errors </div></td>
  </tr>
  <?php 
  }
  ?>
  <tr>
    <td class="footer" colspan="4"></td>
  </tr>
</table>
<p>&nbsp;</p>
<h4>TV Shows</h4>
<table width="98%" border="0" cellspacing="0" cellpadding="0" class="setupTable">
  <tr>
    <th width="2%">&nbsp;</th>
    <th width="36%">Series</th>
    <th width="36%">Name</th>
    <th width="7%">Season</th>
    <th width="7%">Episode</th>
    <th width="12%">Airdate</th>
  </tr>
  <?php
    $i=0;
	while ($row = mysql_fetch_array($scannedTVShowsResult)) {
	$i++;
	$class = "";
	
	if($i % 2 == 0){
		$class = 'class="alt"';
	}
  ?>
  <tr>
    <td <?php echo $class;?>><a href="movies-edit.php?action=Edit&id=<?php echo $row['id'];?>">Edit</a></td>
    <td <?php echo $class;?>><?php echo $row['series_name'];?></td>
    <td <?php echo $class;?>><?php echo $row['name'];?></td>
    <td <?php echo $class;?>><?php echo $row['season'];?></td>
    <td <?php echo $class;?>><?php echo $row['episode'];?></td>
    <td <?php echo $class;?>><?php echo $row['airdate'];?></td>
  </tr>
   <?php 
   }
   
  	if(mysql_num_rows($scannedTVShowsResult) == 0){
  ?>
  <tr>
    <td colspan="6"><div align="center">No tv shows in library </div></td>
  </tr>
  <?php
  }
  ?>
  <tr>
    <td class="footer" colspan="8"></td>
  </tr>
</table>

<!-- Modal Window Code -->
<div id='confirm'>
	<div class='header'><span>default title</span></div>
	<div id="message" class='message'>
	</div>
</div>
<!-- preload the images -->
<div style='display:none'>
	<img src='../images/header.gif' alt='' />
</div></body>
</html>
